package com.mobile.challenge.ui.mine;

import android.os.Build;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import com.king.base.util.SharedPreferencesUtils;
import com.mobile.challenge.LoginActivity;
import com.mobile.challenge.R;
import com.mobile.challenge.databinding.FragmentMineBinding;
import com.mobile.challenge.ui.DashboardActivity;
import com.mobile.frame.mvvmframe.base.BaseFragment;

import static com.mobile.challenge.util.ConstantsKt.HAS_SHOWN_ENROLL_HINT;
import static com.mobile.challenge.util.ConstantsKt.IS_TOUCH_ID_ENROLLED;
import static com.mobile.challenge.util.ConstantsKt.PASSWORD;
import static com.mobile.challenge.util.ConstantsKt.USER_ID;


public class MineFragment extends BaseFragment<MineViewModel, FragmentMineBinding> {

    private boolean isTouchIDEnrolled;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        isTouchIDEnrolled = ((DashboardActivity) requireActivity()).isTouchIDEnrolled();

        if (isTouchIDEnrolled) {
            mBinding.tvEnableFp.setText("Fingerprint Enrolled");
            mBinding.tgSwitch.setChecked(true);
        } else {
            mBinding.tvEnableFp.setText("Enable Fingerprint");
            mBinding.tgSwitch.setChecked(false);
        }

        mBinding.llEnableFp.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onClick(View v) {
                if (!isTouchIDEnrolled) {
                    ((DashboardActivity) getActivity()).initialFingerprintHelper();
                    ((DashboardActivity) getActivity()).openFingerprintLogin();
                } else {
                    clearFPData();
                    isTouchIDEnrolled = false;
                    mBinding.tgSwitch.setChecked(false);
                    mBinding.tvEnableFp.setText("Enable Fingerprint");
                }
            }
        });

        mBinding.tvChangePassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                FragmentTransaction fragmentTransaction = requireActivity().getSupportFragmentManager().beginTransaction();
//                fragmentTransaction.add(ResetPasswordFragment.getInstance(), ResetPasswordFragment.class.getName());
//                fragmentTransaction.commit();

                NavController navController = Navigation.findNavController(requireActivity(), R.id.nav_host_fragment);
                navController.navigate(R.id.navigation_reset);
            }
        });

        ((DashboardActivity) requireActivity()).enrollResult.observe(this, aBoolean -> {
            if (aBoolean) {
                isTouchIDEnrolled = true;
                mBinding.tvEnableFp.setText("Fingerprint Enrolled");
                mBinding.tgSwitch.setChecked(true);
            }
        });

        mBinding.tvSwitchCred.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clearLocalData();
                startActivity(LoginActivity.class);
                requireActivity().finish();
            }
        });

        mBinding.tvQuit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(LoginActivity.class);
                requireActivity().finish();
            }
        });

    }

    private void clearLocalData() {
        SharedPreferencesUtils.put(requireContext(), USER_ID, "");
        SharedPreferencesUtils.put(requireContext(), PASSWORD, "");
        SharedPreferencesUtils.put(requireContext(), IS_TOUCH_ID_ENROLLED, false);
        SharedPreferencesUtils.put(requireContext(), HAS_SHOWN_ENROLL_HINT, false);
        SharedPreferencesUtils.put(requireContext(), "STATUS", "");
    }

    private void clearFPData() {
        SharedPreferencesUtils.put(requireContext(), IS_TOUCH_ID_ENROLLED, false);
    }
}